package com.hyfd.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class TouShiFlowTask {

	Logger log = Logger.getLogger(getClass());
	
	@Autowired
	OrderDao orderDao;
	@Autowired
	RabbitMqProducer mqProducer;//消息队列生产者
	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	
//	@Scheduled(fixedDelay = 60000)
	public void queryTouShiOrder(){
		Map<String,Object> map = new HashMap<String,Object>();
		int flag = 2;
		try{
			String providerId = "0000000048";
			Map<String,Object> channel = providerPhysicalChannelDao.selectByProviderId(providerId);//获取物理通道
			String linkUrl = (String) channel.get("link_url");//充值地址
			String defaultParameter = (String) channel.get("default_parameter");//默认参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			String userid = paramMap.get("userid");//账户名
			String pwd = paramMap.get("pwd");//密码
			String key = paramMap.get("key");//密钥
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", providerId);//千米的运营商id
			param.put("status", 1);
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);
			for(int i = 0 , size = orderList.size() ; i < size ; i++ ){
				Map<String,Object> order = orderList.get(i);
				String orderId = (String) order.get("orderId");
				map.put("orderId", orderId);
				String signStr = "userid"+userid+"pwd"+pwd+"orderid"+orderId+key;//待加密字符串
				String userKey = MD5.ToMD5(signStr);//加密后的串
				String Url = linkUrl + "/query_jkorders.do?userid="+userid+"&pwd="+pwd+"&orderid="+orderId+"&userkey="+userKey;
				String result = ToolHttp.get(false, Url);
				if(result!=null){
					Map<String,String> resultMap = XmlUtils.readXmlToMap(result);
					String state = resultMap.get("state");//充值状态
					String error = resultMap.get("error");//错误代码
					map.put("resultCode", "state:"+state+",error:"+error);
					String Porderid = resultMap.get("Porderid");//上家订单号
					map.put("providerOrderId", Porderid);
					if(error.equals("0")){
						if(state.equals("1")){//成功
							flag = 1;
						}else if(state.equals("2")){//失败
							flag = 0;
						}else{
							continue;
						}
					}else if(error.equals("9999")){
						flag = -1;
					}else{
						flag = -1;
					}
				}
				map.put("status", flag);
				mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
			}
		}catch(Exception e){
			log.error("投石米流量查询出错"+e);
		}
	}
	
}
